/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.comac.ins.common.core.utils.MapstructUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.tenant.helper.TenantHelper;
import com.comac.ins.isfd.domain.IsfdMetadataBusinessRefDatabase;
import com.comac.ins.isfd.domain.bo.IsfdMetadataBusinessRefDatabaseBo;
import com.comac.ins.isfd.domain.vo.IsfdMetadataBusinessRefDatabaseVo;
import com.comac.ins.isfd.mapper.IsfdMetadataBusinessRefDatabaseMapper;
import com.comac.ins.isfd.service.IIsfdMetadataBusinessRefDatabaseService;
import lombok.RequiredArgsConstructor;

/**
 * 元数据管理-业务、要素库关联关系Service业务层处理
 *
 * @author Lion Li
 * @date 2024-09-23
 */
@RequiredArgsConstructor
@Service
public class IsfdMetadataBusinessRefDatabaseServiceImpl implements IIsfdMetadataBusinessRefDatabaseService {

    @Autowired
    private IsfdMetadataBusinessRefDatabaseMapper baseMapper;

    /**
     * 查询元数据管理-业务、要素库关联关系
     *
     * @param id 主键
     * @return 元数据管理-业务、要素库关联关系
     */
    @Override
    public IsfdMetadataBusinessRefDatabaseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询元数据管理-业务、要素库关联关系
     *
     * @param childBusinessId 主键
     * @return 元数据管理-业务、要素库关联关系
     */
    @Override
    public List<IsfdMetadataBusinessRefDatabaseVo> selectByChildBusinessId(Long childBusinessId) {
        return baseMapper.selectVoList(new LambdaQueryWrapper<IsfdMetadataBusinessRefDatabase>()
            .eq(IsfdMetadataBusinessRefDatabase::getChildBusinessId, childBusinessId)
            .eq(TenantHelper.isEnable(), IsfdMetadataBusinessRefDatabase::getTenantId, TenantHelper.getTenantId()));
    }

    @Override
    public void deleteByChildBusinessId(Long childBusinessId) {
        LambdaQueryWrapper<IsfdMetadataBusinessRefDatabase> lqw = new LambdaQueryWrapper<>();
        lqw.eq(IsfdMetadataBusinessRefDatabase::getBusinessId, childBusinessId);

        IsfdMetadataBusinessRefDatabase updateEntity = new IsfdMetadataBusinessRefDatabase();
        updateEntity.setIsDeleted(1L);

        baseMapper.update(updateEntity, lqw);
    }

    /**
     * 分页查询元数据管理-业务、要素库关联关系列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 元数据管理-业务、要素库关联关系分页列表
     */
    @Override
    public TableDataInfo<IsfdMetadataBusinessRefDatabaseVo> queryPageList(IsfdMetadataBusinessRefDatabaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IsfdMetadataBusinessRefDatabase> lqw = buildQueryWrapper(bo);
        Page<IsfdMetadataBusinessRefDatabaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的元数据管理-业务、要素库关联关系列表
     *
     * @param bo 查询条件
     * @return 元数据管理-业务、要素库关联关系列表
     */
    @Override
    public List<IsfdMetadataBusinessRefDatabaseVo> queryList(IsfdMetadataBusinessRefDatabaseBo bo) {
        LambdaQueryWrapper<IsfdMetadataBusinessRefDatabase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IsfdMetadataBusinessRefDatabase> buildQueryWrapper(IsfdMetadataBusinessRefDatabaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IsfdMetadataBusinessRefDatabase> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getBusinessId() != null, IsfdMetadataBusinessRefDatabase::getBusinessId, bo.getBusinessId());
        lqw.eq(bo.getChildBusinessId() != null, IsfdMetadataBusinessRefDatabase::getChildBusinessId, bo.getChildBusinessId());
        lqw.eq(bo.getDatabaseId() != null, IsfdMetadataBusinessRefDatabase::getDatabaseId, bo.getDatabaseId());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessName()), IsfdMetadataBusinessRefDatabase::getBusinessName, bo.getBusinessName());
        lqw.eq(bo.getIsDeleted() != null, IsfdMetadataBusinessRefDatabase::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增元数据管理-业务、要素库关联关系
     *
     * @param bo 元数据管理-业务、要素库关联关系
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(IsfdMetadataBusinessRefDatabaseBo bo) {
        IsfdMetadataBusinessRefDatabase add = MapstructUtils.convert(bo, IsfdMetadataBusinessRefDatabase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改元数据管理-业务、要素库关联关系
     *
     * @param bo 元数据管理-业务、要素库关联关系
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(IsfdMetadataBusinessRefDatabaseBo bo) {
        IsfdMetadataBusinessRefDatabase update = MapstructUtils.convert(bo, IsfdMetadataBusinessRefDatabase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IsfdMetadataBusinessRefDatabase entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除元数据管理-业务、要素库关联关系信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
